package com.chagee.store.operation.server.service;

import cn.hutool.core.util.StrUtil;
import com.chagee.store.operation.server.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.*;

/**
 * @Author : hk
 * @Date : 2024/12/12
 */

@Slf4j
@Service
public class YunLiInvoiceService {

	@Value("${yunLi.appCode}")
	private String appCode;

	@Value("${yunLi.appSecret}")
	private String appSecret;

	@Value("${yunLi.redirectUri}")
	private String redirectUri;

	@Value("${yunLi.url}")
	private String url;

	@Value("${yunLi.userCode}")
	private String loginName;

	public String getPushUrl(String billCode) {
		String billCodeStr = Base64.getEncoder().encodeToString(billCode.getBytes());

		String requestId = UUID.randomUUID().toString();
		Map<String, String> data = new HashMap<>();
		data.put("appCode", appCode);
		data.put("requestId", requestId);
		data.put("loginName", loginName);
		String formattedTimestamp = DateUtil.getTimestamp();
		data.put("timestamp", formattedTimestamp);
		String sign = secretSign(data, appSecret);

		log.info("sso登录sign:{}", sign);

		//String redirect_uri = "https://image-s.xforceplus.com/?id=4698132799113863177&sso=1&pageCode=scanTodo&billCode=RFlGS0QyMDI0MTIwNTAwMTIzMQ==&fixed-tools=1&tenantCode=BWCJ";
		//String url = "https://paas-s.xforceplus.com/api/auth/login/ef49b184bd0643d090670c662a6a2fb9";
		Map<String, String> hashMap = new HashMap<>();
		hashMap.put("billCode",billCodeStr);
		String format = StrUtil.format(redirectUri, hashMap);

		UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(url);
		uriComponentsBuilder.queryParam("loginName", loginName);
		uriComponentsBuilder.queryParam("timestamp", formattedTimestamp);
		uriComponentsBuilder.queryParam("requestId", requestId);
		uriComponentsBuilder.queryParam("sign", sign);
		uriComponentsBuilder.queryParam("redirect_uri", format);

		log.info("YunLiInvoiceService_url:{}", uriComponentsBuilder.toUriString());

		return uriComponentsBuilder.toUriString();
	}


	public static String secretSign(Map<String, String> data, String appSecret) {
		String sortData = signSort(data, appSecret);
		return DigestUtils.md5Hex(sortData).toUpperCase();
	}

	private static String signSort(Map<String, String> data, String appSecret) {
		if (MapUtils.isEmpty(data)) {
			return StringUtils.EMPTY;
		}
		StringBuilder sb = new StringBuilder();
		// 第一步
		data.keySet().stream()
			.filter(name -> !"sign".equals(name) && !"redirect_uri".equals(name))
			.sorted()
			.forEach(name -> sb.append(name).append("=").append(data.get(name)).append("&"));
		// 第二步
		String sortedParams = sb.append("appSecret").append("=").append(appSecret).toString();

		return sortedParams;
	}

}
